// 指向-计时
interface winData2 {
  actionCards: Array<tileData>,
  actionCard: tileData,
  actionCardMap: Object,
  actionCode: number,
  actionIndex: number,
  actionType: string
}
class ChiChoose extends eui.Component{
    private dialogKuang: eui.Image
    private chiData: winData2
    private closeBtn: eui.Image
    private tileOutBlock: eui.Group
    public constructor(){
        super();
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = `
        <e:Skin class="ChiChooseSkin" height="200" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
           <e:Image id="dialogKuang" source="choose-eat_png" percentWidth="100" percentHeight="100" touchEnabled="false" locked="true"/>
           <e:Image id="closeBtn" source="close-btn_png" right="-66" verticalCenter="0" touchEnabled="true" locked="true"/>
           <e:Group id="tileOutBlock" percentWidth="90" percentHeight="90" verticalCenter="20" horizontalCenter="-50">
           </e:Group>
        </e:Skin>`
    }
    private onComplete(){
        var rect: egret.Rectangle = new egret.Rectangle(12,12,150,120)
        this.dialogKuang.scale9Grid = rect
        this.closeBtn.scaleX = this.closeBtn.scaleY = 1.4
        var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
        hLayout.gap = 30
        hLayout.paddingLeft = 30
        hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
        this.tileOutBlock.layout = hLayout
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeChoose, this)
        const chileTip = MyLibrary.createBitmapByName('chiletip_png')
        chileTip.x = 100
        chileTip.y = -50
        this.addChild(chileTip)
    }
    public setChooseData(winMsg) {
        this.chiData = winMsg.canDoActions.find(act => act.actionType === 'CHI')
        console.log(winMsg, this.chiData)
        this.tileOutBlock.removeChildren()
        // const scale = 3 / 4
        const scale = this.chiData.actionCards.length > 3 ? 3 / this.chiData.actionCards.length : 1.2
        this.chiData.actionCards.forEach((card, index) => {
            const cardSuit = new EatSuit(card, index, this.chiData.actionCard)
            cardSuit.scaleX = scale
            cardSuit.scaleY = scale
            this.tileOutBlock.addChild(cardSuit)
            cardSuit.addEventListener('activeSuit', this.activeSuit, this)
            cardSuit.addEventListener('chipai', this.chipai, this)
        })
        if (this.chiData.actionCards.length === 1) {
            this.autoEat()
        }
    }
    public setChooseBuData(winMsg) {
        console.log('setChooseBuData');
        // this.chiData = winMsg.canDoActions.find(act => (act.actionType === 'GONG_GANG' || act.actionType === 'AN_GANG'))
        this.chiData = winMsg.canDoActions.find(act => act.actionType === 'GONG_GANG')        
        console.log(winMsg, this.chiData)
        this.tileOutBlock.removeChildren()
        // const scale = 3 / 4
        const scale = this.chiData.actionCards.length > 3 ? 3 / this.chiData.actionCards.length : 1.2
        this.chiData.actionCards.forEach((card, index) => {
            const cardSuit = new GangSuit(card, index)
            cardSuit.scaleX = scale
            cardSuit.scaleY = scale
            this.tileOutBlock.addChild(cardSuit)
            cardSuit.addEventListener('activeSuit', this.activeSuit, this)
            cardSuit.addEventListener('chipai', this.chipai, this)
            if (this.chiData.actionType === 'GONG_GANG') {
                cardSuit.addEventListener('gangpai', this.gangpai, this)
            }
            // if (this.chiData.actionType === 'AN_GANG') {
            //     cardSuit.addEventListener('gangpai', this.angangpai, this)
            // }
        })
        if (this.chiData.actionCards.length === 1 && this.chiData.actionType === 'GONG_GANG') {
            this.autoGangpai()
        }
        // if (this.chiData.actionCards.length === 1 && this.chiData.actionType === 'AN_GANG') {
        //     this.autoAnGangpai()
        // }
    }

    public setChooseAnGangData(winMsg) {
        console.log('setChooseAnGangData');
        this.chiData = winMsg.canDoActions.find(act => act.actionType === 'AN_GANG')
        console.log(winMsg, this.chiData)
        this.tileOutBlock.removeChildren()
        // const scale = 3 / 4
        const scale = this.chiData.actionCards.length > 3 ? 3 / this.chiData.actionCards.length : 1.2
        this.chiData.actionCards.forEach((card, index) => {
            const cardSuit = new GangSuit(card, index)
            cardSuit.scaleX = scale
            cardSuit.scaleY = scale
            this.tileOutBlock.addChild(cardSuit)
            cardSuit.addEventListener('activeSuit', this.activeSuit, this)
            cardSuit.addEventListener('chipai', this.chipai, this)
            // if (this.chiData.actionType === 'GONG_GANG') {
            //     cardSuit.addEventListener('gangpai', this.gangpai, this)
            // }
            if (this.chiData.actionType === 'AN_GANG') {
                cardSuit.addEventListener('gangpai', this.angangpai, this)
            }
        })
        // if (this.chiData.actionCards.length === 1 && this.chiData.actionType === 'GONG_GANG') {
        //     this.autoGangpai()
        // }
        if (this.chiData.actionCards.length === 1 && this.chiData.actionType === 'AN_GANG') {
            this.autoAnGangpai()
        }
    }

    public setChooseGangData(winMsg) {
        console.log('setChooseGangData', winMsg);
        this.chiData = winMsg.canDoActions.find(act => act.actionType === 'BU_CARD')
        // const replyData = winMsg.canDoActions.find(act => {
        //     const isFind = (act.actionType === 'GONG_GANG' || act.actionType === 'AN_GANG' || act.actionType === 'MING_GANG') &&
        //      (act.actionCards ? act.actionCards[0].cardIndex === this.chiData.actionCards[0].cardIndex : true)
        //     if (isFind) {
        //         console.log(act)
        //     }
        //     return isFind
        // })
        console.log('chiData', this.chiData)
        // console.log('replyData', replyData)
        this.tileOutBlock.removeChildren()
        // const scale = 3 / 4
        const scale = this.chiData.actionCards.length > 3 ? 3 / this.chiData.actionCards.length : 1.2
        this.chiData.actionCards.forEach((card, index) => {
            const cardSuit = new GangSuit(card, index)
            cardSuit.scaleX = scale
            cardSuit.scaleY = scale
            this.tileOutBlock.addChild(cardSuit)
            cardSuit.addEventListener('activeSuit', this.activeSuit, this)
            cardSuit.addEventListener('chipai', this.chipai, this)
            cardSuit.addEventListener('gangpai', function (e) {
                console.log('gangpai', e);
                const centerCard: tileData = e.data.centerCard

                const replyData = winMsg.canDoActions.find(act => {
                    const isFind = (act.actionType === 'GONG_GANG' || act.actionType === 'AN_GANG' || act.actionType === 'MING_GANG') &&
                    (act.actionCards ? act.actionCards.some(card => card.cardIndex === centerCard.cardIndex) : true)
                    if (isFind) {
                        console.log(act)
                    }
                    return isFind
                })

                console.log('replyData', replyData)

                let data = {
                    commandCode: "20007",
                    version: "0.1",
                    actionCode: replyData.actionCode,
                    card: centerCard,
                    step: GameData.step
                };
                Message.sendMsgToServer(data);
                this.dispatchEvent(new egret.Event('closeChi'))
            }, this)
        })
        if (this.chiData.actionCards.length === 1) {
            const centerCard: tileData = this.chiData.actionCards[0]
            let replyData2 = winMsg.canDoActions.find(act => {
                const isFind = (act.actionType === 'GONG_GANG' || act.actionType === 'AN_GANG' || act.actionType === 'MING_GANG') &&
                (act.actionCards ? act.actionCards[0].cardIndex === centerCard.cardIndex : true)
                if (isFind) {
                    console.log(act)
                }
                return isFind
            })
            
            let data = {
                commandCode: "20007",
                version: "0.1",
                actionCode: replyData2.actionCode,
                card: centerCard,
                step: GameData.step
            };
            Message.sendMsgToServer(data);
            this.dispatchEvent(new egret.Event('closeChi'))
        }
    }


    public setChooseAngangData(winMsg) {
        console.log('setChooseAnGangData');
        this.chiData = winMsg.canDoActions.find(act => act.actionType === 'BU_CARD')
        const replyData = winMsg.canDoActions.find(act => {
            return (act.actionType === 'AN_GANG') && (act.actionCards ? act.actionCards[0].cardIndex === this.chiData.actionCards[0].cardIndex : true)
        })
        console.log(winMsg, this.chiData)
        console.log('replyData', replyData)
        this.tileOutBlock.removeChildren()
        // const scale = 3 / 4
        const scale = this.chiData.actionCards.length > 3 ? 3 / this.chiData.actionCards.length : 1.2
        this.chiData.actionCards.forEach((card, index) => {
            const cardSuit = new GangSuit(card, index)
            cardSuit.scaleX = scale
            cardSuit.scaleY = scale
            this.tileOutBlock.addChild(cardSuit)
            cardSuit.addEventListener('activeSuit', this.activeSuit, this)
            cardSuit.addEventListener('chipai', this.chipai, this)
            cardSuit.addEventListener('gangpai', function (e) {
                console.log('gangpai', e);
                const centerCard: tileData = e.data.centerCard
                let data = {
                    commandCode: "20007",
                    version: "0.1",
                    actionCode: replyData.actionCode,
                    card: centerCard,
                    step: GameData.step
                };
                Message.sendMsgToServer(data);
                this.dispatchEvent(new egret.Event('closeChi'))
            }, this)
        })
        if (this.chiData.actionCards.length === 1) {
            const centerCard: tileData = this.chiData.actionCards[0]
            let data = {
                commandCode: "20007",
                version: "0.1",
                actionCode: replyData.actionCode,
                card: centerCard,
                step: GameData.step
            };
            Message.sendMsgToServer(data);
            this.dispatchEvent(new egret.Event('closeChi'))
        }
    }

    public autoEat() {
        const centerCard: tileData = this.chiData.actionCards[0]
        let data = {
            commandCode: "20005",
            version: "0.1",
            actionCode: 1,
            card: centerCard,
            step: GameData.step
        };
        Message.sendMsgToServer(data)
        this.dispatchEvent(new egret.Event('closeChi'))
    }
    public activeSuit(e) {
        for (let i = 0; i < this.tileOutBlock.numElements; i++) {
            const suit = <EatSuit>this.tileOutBlock.getElementAt(i)
            suit.active = i === e.data.index
        }
    }
    public chipai(e) {
        const centerCard: tileData = this.chiData.actionCards[e.data.index]
        let data = {
            commandCode: "20005",
            version: "0.1",
            actionCode: 1,
            card: centerCard,
            step: GameData.step
        };
	    Message.sendMsgToServer(data)
        this.dispatchEvent(new egret.Event('closeChi'))
    }
    public autoGangpai() {
        const centerCard: tileData = this.chiData.actionCards[0]
        let data = {
			commandCode: "20003",
			version: "0.1",
			actionCode: 6,
            card: centerCard,
            step: GameData.step
		};
	    Message.sendMsgToServer(data);
        this.dispatchEvent(new egret.Event('closeChi'))
    }
    public autoAnGangpai() {
        console.log('autoAnGangpai!');
        const centerCard: tileData = this.chiData.actionCards[0]
        let data = {
			commandCode: "20003",
			version: "0.1",
			actionCode: 5,
            card: centerCard,
            step: GameData.step
		};
	    Message.sendMsgToServer(data);
        this.dispatchEvent(new egret.Event('closeChi'))
    }
    public angangpai(e) {
        const centerCard: tileData = e.data.centerCard
        let data = {
			commandCode: "20003",
			version: "0.1",
			actionCode: 5,
            card: centerCard,
            step: GameData.step
		};
	    Message.sendMsgToServer(data);
        this.dispatchEvent(new egret.Event('closeChi'))
    }
    public gangpai(e) {
        const centerCard: tileData = e.data.centerCard
        let data = {
			commandCode: "20003",
			version: "0.1",
			actionCode: 6,
            card: centerCard,
            step: GameData.step
		};
	    Message.sendMsgToServer(data);
        this.dispatchEvent(new egret.Event('closeChi'))
    }
    public closeChoose() {
        this.dispatchEvent(new egret.Event('closeChi'))
    }
}

class EatSuit extends eui.Component{
    private tileOutBlock: eui.Group
    private dialogKuang: eui.Image
    private centerCard: tileData
    private chiCard: tileData
    public active: boolean = false
    public doubleClick: boolean = false
    private doublePressTimer: number
    public index: number
    public constructor(card, index, chiCard){
        super();
        this.index = index
        this.centerCard = card
        this.chiCard = chiCard
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = `
        <e:Skin class="EatSuitSkin" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
           <e:Image includeInLayout="false" id="dialogKuang" source="choosen_png" visible="{active}"
            percentWidth="100" percentHeight="100" x="-5" y="-5" touchEnabled="false" locked="true"/>
           <e:Group id="tileOutBlock">
           </e:Group>
        </e:Skin>`
    }
    private onComplete(){
        var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
        hLayout.gap = -1
        hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
        this.tileOutBlock.layout = hLayout
        this.setChooseData()
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.activeSuit, this)
    }
    public activeSuit(e: TouchEvent) {
        this.dispatchEvent(new egret.Event('chipai', false, false, {index: this.index}))
        // // this.active = true
        // this.dispatchEvent(new egret.Event('activeSuit', false, false, {index: this.index}))
        // // this.dispatchEventWith('activeSuit', false, {index: this.index})
        // //双击检测
        // if (this.doubleClick) {
        //     //出牌       
        //     console.log('吃牌啦!')
        //     this.dispatchEvent(new egret.Event('chipai', false, false, {index: this.index}))
        //     return
        // }
        // this.doubleClick = true
        // this.doublePressTimer = egret.setTimeout(() => {
        //     egret.clearTimeout(this.doublePressTimer)
        //     this.doublePressTimer = null
        //     this.doubleClick = false
        // }, this, 300)
    }
    public setChooseData() {
        console.log('this.chiCard', this.chiCard);
        for (let i = 0; i < 3; i++) {
            const tile = new Tile()
            const newData = {
                cardIndex: this.centerCard.cardIndex, 
                count: this.centerCard.count + i - 1, 
                suitType: this.centerCard.suitType
            }
            tile.setData(newData)
            console.log('newData.count', newData.count);
            if (this.chiCard.count === newData.count) {
                tile.setfaguang(true)
            }
            this.tileOutBlock.addChild(tile)
        }
    }
}

class GangSuit extends eui.Component{
    private tileOutBlock: eui.Group
    private dialogKuang: eui.Image
    private centerCard: tileData
    public active: boolean = false
    public doubleClick: boolean = false
    private doublePressTimer: number
    public index: number
    public constructor(card, index){
        super();
        this.index = index
        this.centerCard = card
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = `
        <e:Skin class="EatSuitSkin" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
           <e:Image includeInLayout="false" id="dialogKuang" source="choosen_png" visible="{active}"
            percentWidth="100" percentHeight="100" x="-5" y="-5" touchEnabled="false" locked="true"/>
           <e:Group id="tileOutBlock">
           </e:Group>
        </e:Skin>`
    }
    private onComplete(){
        var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
        hLayout.gap = -1
        hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
        this.tileOutBlock.layout = hLayout
        this.setChooseData()
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.activeSuit, this)
    }
    public activeSuit(e: TouchEvent) {
        this.dispatchEvent(new egret.Event('gangpai', false, false, {centerCard: this.centerCard}))
    }
    public setChooseData() {
        for (let i = 0; i < 4; i++) {
            const tile = new Tile()
            const newData = {
                cardIndex: this.centerCard.cardIndex, 
                count: this.centerCard.count, 
                suitType: this.centerCard.suitType
            }
            tile.setData(newData)
            this.tileOutBlock.addChild(tile)
        }
    }
}